package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    private IDepartmentService departmentService;

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    //处理查询所有部门的请求,响应HTML
    //department/*.do
    @RequiredPermission(name = "部门页面", expression = "department:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        System.out.println(qo);
        IPage<Department> query = departmentService.query(qo);
        model.addAttribute("pageInfo", query);
        return "department/list";// 找视图 /WEB-INF/views/department/list.jsp
    }

    //处理删除请求
    //  department/delete.dp?id=1,  重定向到  /department/list.do
    @RequiredPermission(name = "部门删除", expression = "department:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            departmentService.delete(id);
        }
        //让浏览器重新发送一次请求
        return "redirect:/department/list";
    }

    //去新增或者去修改页面
    @RequiredPermission(name = "部门新增/修改", expression = "department:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {//表示去修改
            Department department = departmentService.get(id);
            model.addAttribute("department", department);
        }
        return "department/input";//让浏览器重新发一次请求
    }

    //新增或者修改
    @RequiredPermission(name = "部门新增/修改", expression = "department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department) {
        if (department.getId() == null) {
            //新增
            departmentService.save(department);
        } else {
            //修改
            departmentService.update(department);
        }
        return "redirect:/department/list";//让浏览器重新发送一次请求
    }

}


